package com.example.content.fragment

import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.content.*
import com.example.content.adapter.recycle.FunAction
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.HomeItemEntity
import com.example.content.databinding.FragmentCommonListBinding
import com.example.content.fragment.common.CommonListBindListener
import com.example.content.fragment.common.FlowSelectListener
import com.example.content.manager.GlobalMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.ProductInfo
import com.example.content.repository.bean.UserActionReq
import com.example.content.viewmodule.main.CommonHomeViewModel

/**
 * 通用主页
 */
class CommonListFragment(val pageType: Int, val sameCity: Boolean) :
    BaseMvvmFragment<FragmentCommonListBinding, CommonHomeViewModel>() {

    private lateinit var adapter: RecyclerAdapter<ProductInfo>
    private var resourceType = 3
    private var creationType = 2


    override fun initView() {
        dataBinding.refresh.setEnableLoadMore(true)
        dataBinding.refresh.setOnRefreshListener {
            queryData()
        }
        dataBinding.refresh.setOnLoadMoreListener {
            loadMoreData()
        }
        val commonListBindListener = CommonListBindListener(this)
        commonListBindListener.flowSelectListener = object : FlowSelectListener {
            override fun onSelect(resType: Int, creatType: Int) {
                resourceType = resType
                creationType = creatType
                queryData()
            }
        }
        adapter = RecyclerAdapter(commonListBindListener)

        adapter.addItemViewByType(HOME_BANNER, R.layout.banner_layout)
        adapter.addItemViewByType(PHOTO_LIST_TYPE, R.layout.item_photo_layout)
        adapter.addItemViewByType(VIDEO_TYPE, R.layout.item_video_layout)
        adapter.addItemViewByType(FLOW_ITEM_TYPE, R.layout.hori_recycler_layout)

        val layoutManager = LinearLayoutManager(context)
        dataBinding.recyclerView.layoutManager = layoutManager
        dataBinding.recyclerView.adapter = adapter
        adapter.setEmptyView(R.layout.empty_pager_layout)

        GlobalMgr.uploadProduct.observe(this, {
            if (it) {
                queryData()
            }
        })
    }

    override fun getLayoutId() = R.layout.fragment_common_list

    override fun initListener() {
        viewModel.recommendData.observe(this, {
            val isRefresh = it.first
            val listData = it.second
            if (isRefresh) {
                Log.i("freshlistener", "finish refresh")
                dataBinding.refresh.finishRefresh()
                adapter.data.clear()
            } else {
                Log.i("freshlistener", "finish loadMore")
                dataBinding.refresh.finishLoadMore()
            }
            if (listData.isEmpty()) {
                if (isRefresh) {
                    adapter.updateData(emptyList())
                }
            } else {
                // TODO: 2/27/21 增加测试
                if (pageType == REMD_ALL) {
                    adapter.addData(HomeItemEntity(HOME_BANNER, listData[0]))
                }

                if (pageType == REMD_MAN || pageType == REMD_FEMAIL) {
                    adapter.addData(HomeItemEntity(FLOW_ITEM_TYPE, listData[0]))
                }

                for (recommend in listData) {
                    val data: HomeItemEntity = when (recommend.resource_type) {
                        RESOURCE_TYPE_IMG -> {
                            HomeItemEntity(PHOTO_LIST_TYPE, recommend)
                        }
                        RESOURCE_TYPE_VIDEO -> {
                            HomeItemEntity(VIDEO_TYPE, recommend)
                        }
                        else -> {
                            HomeItemEntity(PHOTO_LIST_TYPE, recommend)
                        }
                    }
                    data.funAction = object : FunAction {
                        override fun follow(followReq: FollowReq) {
                            viewModel.follow(followReq)
                        }

                        override fun userAction(userActionReq: UserActionReq) {
                            viewModel.userAction(userActionReq)
                        }
                    }
                    adapter.addData(data)
                }
                adapter.notifyDataSetChanged()
            }
        })
    }

    override fun queryData() {
        viewModel.refresh(pageType, resourceType, creationType, sameCity)
    }

    private fun loadMoreData() {
        viewModel.loadMore(pageType, resourceType, creationType, sameCity)
    }

    companion object {
        @JvmStatic
        fun newInstance(pageType: Int, sameCity: Boolean) = CommonListFragment(pageType, sameCity)
    }

    override fun getViewModuleClass(): Class<CommonHomeViewModel> {
        return CommonHomeViewModel::class.java
    }
}